#!/usr/local/bin/python3
# *_* coding: UTF-8 *_*
# @IDE: PyCharm
# @Version: Python3.97
# @Author: Kendrick.Kc
# @Email: 509556985@qq.com
# @File: messages.py
# @Inst: 这里是模块名描述
# @Time: 2023/6/23 13:37
# -----


import logging
from logging.config import dictConfig
from django.core.mail import send_mail
from django.core.cache import cache
from ROOT import settings
from utils.localhost_ip import get_localhost_ip_address

logging.raiseExceptions = False


class Messages:

    def __init__(self, title, pan_opening):
        self.pan_opening = pan_opening  # 盘面类型
        self.title = title
        self.localhost_ip = get_localhost_ip_address()
        dictConfig(cache.get('logging'))
        self.logger = logging.getLogger('django')

    def logger_info(self, content):
        dictConfig(cache.get('logging'))
        self.logger = logging.getLogger('django')
        self.logger.info(f"[{self.pan_opening}][{self.title}]: {content}")

    def logger_warning(self, content):
        dictConfig(cache.get('logging'))
        self.logger = logging.getLogger('django')
        self.logger.warning(f"[{self.pan_opening}][{self.title}]: {content}")

    def logger_error(self, content):
        """ 检测到error日志系统自动发送邮件 """
        dictConfig(cache.get('logging'))
        self.logger = logging.getLogger('django')
        self.logger.error(f"[{self.localhost_ip}][{self.pan_opening}][{self.title}]: 程序退出. \n {content}")
        exit()

    def send_email(self, content, title="量化交易机器人系统[错误]提醒"):
        """
        发送自定义邮件
        注意：这里的邮件与logging的邮件功能不为同一个接口
        :param content: 邮件内容
        :param title: 邮件标题
        :return:
        """
        content = f"[{self.localhost_ip}][{self.pan_opening}][{self.title}]: {content}"
        send_mail(title, content, settings.EMAIL_HOST_USER, cache.get('email'))
